/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package logica.jpaControllers;

import Entidades.Estadooperario;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import Entidades.Operario;
import java.util.ArrayList;
import java.util.Collection;
import logica.jpaControllers.exceptions.IllegalOrphanException;
import logica.jpaControllers.exceptions.NonexistentEntityException;

/**
 *
 * @author Anali
 */
public class EstadooperarioJpaController {

    public EstadooperarioJpaController() {
        emf = Persistence.createEntityManagerFactory("GestorPesajeJpaPU2");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Estadooperario estadooperario) {
        if (estadooperario.getOperarioCollection() == null) {
            estadooperario.setOperarioCollection(new ArrayList<Operario>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Collection<Operario> attachedOperarioCollection = new ArrayList<Operario>();
            for (Operario operarioCollectionOperarioToAttach : estadooperario.getOperarioCollection()) {
                operarioCollectionOperarioToAttach = em.getReference(operarioCollectionOperarioToAttach.getClass(), operarioCollectionOperarioToAttach.getIdoperario());
                attachedOperarioCollection.add(operarioCollectionOperarioToAttach);
            }
            estadooperario.setOperarioCollection(attachedOperarioCollection);
            em.persist(estadooperario);
            for (Operario operarioCollectionOperario : estadooperario.getOperarioCollection()) {
                Estadooperario oldEstadooperarioOfOperarioCollectionOperario = operarioCollectionOperario.getEstadooperario();
                operarioCollectionOperario.setEstadooperario(estadooperario);
                operarioCollectionOperario = em.merge(operarioCollectionOperario);
                if (oldEstadooperarioOfOperarioCollectionOperario != null) {
                    oldEstadooperarioOfOperarioCollectionOperario.getOperarioCollection().remove(operarioCollectionOperario);
                    oldEstadooperarioOfOperarioCollectionOperario = em.merge(oldEstadooperarioOfOperarioCollectionOperario);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Estadooperario estadooperario) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Estadooperario persistentEstadooperario = em.find(Estadooperario.class, estadooperario.getIdestadooperario());
            Collection<Operario> operarioCollectionOld = persistentEstadooperario.getOperarioCollection();
            Collection<Operario> operarioCollectionNew = estadooperario.getOperarioCollection();
            List<String> illegalOrphanMessages = null;
            for (Operario operarioCollectionOldOperario : operarioCollectionOld) {
                if (!operarioCollectionNew.contains(operarioCollectionOldOperario)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Operario " + operarioCollectionOldOperario + " since its estadooperario field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            Collection<Operario> attachedOperarioCollectionNew = new ArrayList<Operario>();
            for (Operario operarioCollectionNewOperarioToAttach : operarioCollectionNew) {
                operarioCollectionNewOperarioToAttach = em.getReference(operarioCollectionNewOperarioToAttach.getClass(), operarioCollectionNewOperarioToAttach.getIdoperario());
                attachedOperarioCollectionNew.add(operarioCollectionNewOperarioToAttach);
            }
            operarioCollectionNew = attachedOperarioCollectionNew;
            estadooperario.setOperarioCollection(operarioCollectionNew);
            estadooperario = em.merge(estadooperario);
            for (Operario operarioCollectionNewOperario : operarioCollectionNew) {
                if (!operarioCollectionOld.contains(operarioCollectionNewOperario)) {
                    Estadooperario oldEstadooperarioOfOperarioCollectionNewOperario = operarioCollectionNewOperario.getEstadooperario();
                    operarioCollectionNewOperario.setEstadooperario(estadooperario);
                    operarioCollectionNewOperario = em.merge(operarioCollectionNewOperario);
                    if (oldEstadooperarioOfOperarioCollectionNewOperario != null && !oldEstadooperarioOfOperarioCollectionNewOperario.equals(estadooperario)) {
                        oldEstadooperarioOfOperarioCollectionNewOperario.getOperarioCollection().remove(operarioCollectionNewOperario);
                        oldEstadooperarioOfOperarioCollectionNewOperario = em.merge(oldEstadooperarioOfOperarioCollectionNewOperario);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = estadooperario.getIdestadooperario();
                if (findEstadooperario(id) == null) {
                    throw new NonexistentEntityException("The estadooperario with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Estadooperario estadooperario;
            try {
                estadooperario = em.getReference(Estadooperario.class, id);
                estadooperario.getIdestadooperario();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The estadooperario with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            Collection<Operario> operarioCollectionOrphanCheck = estadooperario.getOperarioCollection();
            for (Operario operarioCollectionOrphanCheckOperario : operarioCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Estadooperario (" + estadooperario + ") cannot be destroyed since the Operario " + operarioCollectionOrphanCheckOperario + " in its operarioCollection field has a non-nullable estadooperario field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            em.remove(estadooperario);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Estadooperario> findEstadooperarioEntities() {
        return findEstadooperarioEntities(true, -1, -1);
    }

    public List<Estadooperario> findEstadooperarioEntities(int maxResults, int firstResult) {
        return findEstadooperarioEntities(false, maxResults, firstResult);
    }

    private List<Estadooperario> findEstadooperarioEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from Estadooperario as o");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Estadooperario findEstadooperario(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Estadooperario.class, id);
        } finally {
            em.close();
        }
    }

    public int getEstadooperarioCount() {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select count(o) from Estadooperario as o");
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

    public Estadooperario findEstadooperario_x_nombre(String nom) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select u from Estadooperario u where u.nombre = " + "'" + nom + "'");
            return (Estadooperario) q.getSingleResult();
        } finally {
            em.close();
        }
    }
}
